package com.xiaodimall.beforesys.controller;


import net.minidev.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * @author wyc
 * @description
 * @ClassName WeatherController
 * @date 2022/11/17 10:54
 */
@Controller
public class WeatherController {

    @Value("${weather.cityUrl}")
    String cityUrl;


    @Value("${weather.weatherUrl}")
    String weatherUrl;
    @Value("${weather.weatherUrl2}")
    String weatherUrl2;

    //注入远程服务
    @Resource
    RestTemplate restTemplate;
    //介绍getForObject(String url,响应的数据类型，0 bject··)

    //获取城市
    @GetMapping("/getCity")
    @ResponseBody
    public JSONObject getCity(){
        //使用restTemplute发送UrL这个请求，将返回的JS0W格式的数据封装JS0 NObject
        return restTemplate.getForObject(cityUrl, JSONObject.class);
    }

    @GetMapping("/getWeatherByCity")
    @ResponseBody
    public JSONObject getWeatherByCity(String city){
        String url=weatherUrl+city+"&"+weatherUrl2;
        //获取天气信息
        return restTemplate.getForObject(url,JSONObject.class);

    }

}
